﻿
Partial Class WebForm_MySlidingMenuGroup
    Inherits System.Web.UI.UserControl

    Protected Sub rep_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rep.ItemCommand
        If e.CommandName = "lknMenuName" Then
            'Me.SelectedMenuName = TryCast(e.CommandSource, LinkButton).Text
            'Response.Redirect("~\WebForm\" + e.CommandArgument)
            'Response.Redirect(String.Format("~\{0}\{1}", Me.ProgramDirectory, e.CommandArgument))
            Response.Redirect(e.CommandArgument)
        End If
    End Sub

    Protected Sub rep_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rep.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            'Response.Write(Me.SelectedMenuName + TryCast(e.Item.FindControl("lknMenuName"), LinkButton).Text + "<br>")
            'If Me.SelectedMenuName = TryCast(e.Item.FindControl("lknMenuName"), LinkButton).Text Then

            'If TryCast(e.Item.FindControl("lknMenuName"), LinkButton).CommandArgument = Request.RawUrl.Split("/")(Request.RawUrl.Split("/").Length - 1) Then
            If DataBinder.Eval(e.Item.DataItem, "ProgramID") = Request("ProgramID") Then
                ''TryCast(e.Item.FindControl("lknMenuName"), LinkButton).BorderColor = System.Drawing.Color.Red
                'TryCast(e.Item.FindControl("lknMenuName"), LinkButton).Attributes.Add("style", "color:Red;font-size: 14px")
                TryCast(e.Item.FindControl("aMenuName"), HtmlAnchor).Attributes.Add("style", "color:Red;font-size: 14px")
                Me.Collapsed = False
            Else
                'TryCast(e.Item.FindControl("lknMenuName"), LinkButton).Attributes.Add("style", "color:Blue;font-size: 14px")
                TryCast(e.Item.FindControl("aMenuName"), HtmlAnchor).Attributes.Add("style", "color:Blue;font-size: 14px")
                'TryCast(e.Item.FindControl("lknMenuName"), LinkButton).BorderColor = System.Drawing.Color.Blue
            End If
            Dim url As String = ""
            If DataBinder.Eval(e.Item.DataItem, "Target") = "_self" Then
                url = String.Format("~\{0}\{1}?ProgramID={2}", Me.ProgramDirectory, DataBinder.Eval(e.Item.DataItem, "Url"), DataBinder.Eval(e.Item.DataItem, "ProgramID"))


            Else
                url = Me.ResolveUrl(DataBinder.Eval(e.Item.DataItem, "Url"))
            End If
            TryCast(e.Item.FindControl("aMenuName"), HtmlAnchor).HRef = Me.ResolveUrl(url)
            TryCast(e.Item.FindControl("aMenuName"), HtmlAnchor).Target = DataBinder.Eval(e.Item.DataItem, "target")
            TryCast(e.Item.FindControl("aMenuName"), HtmlAnchor).InnerText = DataBinder.Eval(e.Item.DataItem, "ProgramName")
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.cpeDemo.Collapsed = Me.Collapsed
            Me.cpeDemo.ClientState = Me.Collapsed.ToString()

            'Response.Write(Convert.ChangeType(Me.cpeDemo.ClientState, TypeCode.Boolean))
        Else
            'Response.Write(Convert.ChangeType(Me.cpeDemo.ClientState, TypeCode.Boolean))
            'Response.Write(Me.cpeDemo.Collapsed)
            Me.Collapsed = Convert.ChangeType(Me.cpeDemo.ClientState, TypeCode.Boolean)

            'Response.Write(Me.Collapsed)
        End If
    End Sub
End Class

Partial Class WebForm_MySlidingMenuGroup
    Public Sub BindGV(ByVal dt As Data.DataTable)
        'Me.rep.DataSource = dt
        'Me.rep.DataBind()
        If Me.ListType = "L" Or Me.ListType = "" Then
            Me.rep.DataSource = dt
            Me.rep.DataBind()

            Me.mvList.SetActiveView(Me.vwList)
        ElseIf Me.ListType = "T" Then
            Me.AddNodes(Me.tv.Nodes, dt)
            Me.mvList.SetActiveView(Me.vwTree)
            'Me.tv.CollapseAll()
        End If

    End Sub
    Public Sub AddNodes(ByVal childNodes As TreeNodeCollection, ByVal dt As Data.DataTable)
        For Each dr As Data.DataRow In dt.Rows
            Dim node As New TreeNode(dr("ProgramName"))

            node.Expanded = False

            If Not TypeOf dr("Url") Is DBNull And dr("Url") <> "" Then

                If dr("Target") = "_blank" Then
                    'If dr("Url").ToString().Contains("http:") = True Or dr("Url").ToString().Contains("https:") = True Or dr("Url").ToString().Contains("file:") = True _
                    'Or dr("Url").ToString().Contains("\\") = True Then
                    'If TypeOf dr("ReferrerUrl") Is DBNull Then
                    '    node.NavigateUrl = dr("Url")
                    'Else
                    '    node.NavigateUrl = String.Format("{0}?url={1}", dr("ReferrerUrl"), Server.UrlEncode(dr("Url")))
                    'End If
                    node.NavigateUrl = dr("Url")
                    node.Target = "_blank"
                Else
                    node.NavigateUrl = String.Format("~\{0}\{1}?ProgramID={2}", Me.ProgramDirectory, dr("Url"), dr("ProgramID"))
                    node.Target = dr("target")

                    If dr("ProgramID") = Request("ProgramID") Then
                        node.Selected = True
                        node.Expanded = True
                    End If

                End If
            End If

            childNodes.Add(node)

            If node.Expanded.Value = True Then
                Dim parent As TreeNode = node.Parent
                While parent IsNot Nothing
                    parent.Expanded = True
                    parent = parent.Parent
                End While
            End If

            Dim e As New TreeViewNodeAddedEventArgs
            e.Node = node
            e.ProgramID = dr("ProgramID")

            RaiseEvent TreeViewNodeAdded(Me, e)
        Next
    End Sub

    Public Property ListType() As String
        Get
            If ViewState("ListType") Is Nothing Then
                Return "L"
            End If

            Return ViewState("ListType")
        End Get
        Set(ByVal value As String)
            ViewState("ListType") = value
        End Set
    End Property

    Public Property ProgramID() As String
        Get
            Return ViewState("ProgramID")
        End Get
        Set(ByVal value As String)
            ViewState("ProgramID") = value
        End Set
    End Property

    Public Property MenuName() As String
        Get
            Return Me.lblMenuName.Text
        End Get
        Set(ByVal value As String)
            Me.lblMenuName.Text = value
        End Set
    End Property

    Public Property ProgramDirectory() As String
        Get
            If ViewState("ProgramDirectory") Is Nothing Then
                Return "WebForm"
            End If

            Return ViewState("ProgramDirectory")
        End Get
        Set(ByVal value As String)
            ViewState("ProgramDirectory") = value
        End Set
    End Property

    'Private Property SelectedMenuName() As String
    '    Get
    '        Return Session("SelectedMenuName")
    '    End Get
    '    Set(ByVal value As String)
    '        Session("SelectedMenuName") = value
    '    End Set
    'End Property

    Private Property Collapsed() As Boolean
        Get
            If Session(Me.UniqueID + "Collapsed") Is Nothing Then
                Return False
            End If

            Return Session(Me.UniqueID + "Collapsed")
        End Get
        Set(ByVal value As Boolean)
            Session(Me.UniqueID + "Collapsed") = value
        End Set
    End Property
    Public Event TreeViewNodeAdded(ByVal source As WebForm_MySlidingMenuGroup, ByVal e As TreeViewNodeAddedEventArgs)

    Public Class TreeViewNodeAddedEventArgs
        Inherits EventArgs

        Public Node As TreeNode
        Public ProgramID As String
    End Class
End Class